package org.lw.ssm.mapper;



import org.apache.ibatis.annotations.*;
import org.lw.ssm.Entity.Department;
import org.lw.ssm.Entity.Employee;

import java.util.List;

@Mapper
public interface DepartmentMapper {

    @Select("SELECT * FROM department WHERE id = #{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "employees", column = "id",
                    many = @Many(select = "getEmployeesByDepartmentId")) // 一对多映射
    })
    Department getDepartmentById(Long id);

    @Select("SELECT * FROM department")
    List<Department> getAllDepartments();

    @Insert("INSERT INTO department(name) VALUES(#{name})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertDepartment(Department department);

    @Update("UPDATE department SET name = #{name} WHERE id = #{id}")
    void updateDepartment(Department department);

    @Delete("DELETE FROM department WHERE id = #{id}")
    void deleteDepartment(Long id);

    @Select("SELECT * FROM employee WHERE department_id = #{departmentId}")
    List<Employee> getEmployeesByDepartmentId(Long departmentId);
}


